package cn.wolfcde.luowowo.website.web.controller;


import cn.wolfcode.luowowo.airticket.domain.FlightDestination;
import cn.wolfcode.luowowo.airticket.domain.FlightMessage;
import cn.wolfcode.luowowo.airticket.service.IFlightDestinationService;
import cn.wolfcode.luowowo.airticket.service.IFlightMessageService;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("flight")
public class FlightController {

    @Reference
    private IFlightMessageService flightMessageService;

    @Reference
    private IFlightDestinationService flightDestinationService;

    @RequestMapping("/index")
    public String flightIndex(Model model) {

        //hotFlights 热门航班城市
        List<FlightDestination> hotFlights = flightDestinationService.queryisHot();

        //initialA 以A-E开头的城市
        List<FlightDestination> initialA = flightDestinationService.queryInitial("A,B,C,D,E");

        //initialF 以F-P开头的城市
        List<FlightDestination> initialF = flightDestinationService.queryInitial("F,G,H,I,J");

        //initialK 以K-O开头的城市
        List<FlightDestination> initialK = flightDestinationService.queryInitial("K,L,M,N,O,P");

        //initialQ 以Q-W开头的城市
        List<FlightDestination> initialQ = flightDestinationService.queryInitial("Q,R,S,T,W");

        //initialX 以x-z开头的城市
        List<FlightDestination> initialX = flightDestinationService.queryInitial("X,Y,Z");

        model.addAttribute("hotFlights", hotFlights);
        model.addAttribute("initialA", initialA);
        model.addAttribute("initialF", initialF);
        model.addAttribute("initialK", initialK);
        model.addAttribute("initialQ", initialQ);
        model.addAttribute("initialX", initialX);

        return "flight/index";

    }

    /**
     * 查询指定行程的机票信息
     *
     * @param model
     * @param orgCity
     * @param dstCity
     * @param depTime
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public AjaxResult flightSearch(Model model, String orgCity, String dstCity, @DateTimeFormat(pattern = "yyyy-MM-dd") Date depTime) {

        AjaxResult ajaxResult = new AjaxResult();

        List<FlightMessage> fd = flightMessageService.selectFlightMessage(orgCity, dstCity, depTime);

        ajaxResult.setResult(fd);

        return ajaxResult;
    }

}
